#include<bits/stdc++.h>
#define int long long
using namespace std;
template <typename T> inline void read(T &x) {
    x = 0; char ch = getchar(); int f = 1;
    while (!isdigit(ch) && ch ^ '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); x *= f;
}
inline void init(){
	freopen("light.in","r",stdin);
	freopen("light.out","w",stdout);
}
const int Maxn=405;
int n;
struct node{int data,id;}a[Maxn];
int vis[Maxn];
int ans=-1e18;
int head[Maxn],to[Maxn<<1],nxt[Maxn<<1],cnt1;
inline void add(int u,int v){
	to[++cnt1]=v;
	nxt[cnt1]=head[u];
	head[u]=cnt1;
}
void dfs(int x,int sum){
	if(x>n){ans=max(ans,sum);return;}
	for(int i=1;i<=n;i++)
		if(!vis[i]){
			vis[i]=1;
			for(int j=head[i];j;j=nxt[j])a[to[j]].data+=a[i].data;
			dfs(x+1,sum+a[i].data);
			for(int j=head[i];j;j=nxt[j])a[to[j]].data-=a[i].data;
			vis[i]=0;
		}
}
int op1=1,op2=1;
inline bool cmp(node a,node b){return a.data>b.data;}
signed main(){
	init();
	read(n);
	for(int i=1;i<=n;i++)read(a[i].data),a[i].id=i;
	for(int i=1;i<n;i++){
		int u,v;read(u);read(v);
		if(u>v)swap(u,v);
		add(u,v);add(v,u);
		if(u!=1)op2=0;
		if(u-v!=1)op1=0;
	}
	if(n<=10)dfs(1,0),printf("%lld",ans);
	else{
		if(op2){
			int data=a[1].data;
			sort(a+1,a+1+n,cmp);
			int sum=0;
			for(int i=1;i<=n;i++){
				if(a[i].id==1)continue;
				int sum1=0;
				for(int j=1;j<=n;j++)
					if(!vis[a[j].id]&&a[j].id!=1)sum1+=a[j].data+data+sum;
				ans=max(ans,sum1+sum*2+data);
				sum+=a[i].data;vis[a[i].id]=1;
			}
			ans=max(ans,sum*2+data);
			printf("%lld",ans);
		}
	}
	return 0;
}

